<template>
  <div class="i-table-no-border">
    <div>
      <table-btns
        :tableTopOperateData="listData.tableTopOperate"
        :tableTopBatchOperate="listData.tableTopBatchOperate"
        :selectedData="selectedData"
        :requestApi="listData.requestApi"
        :basePath="basePath"
        @exportData="exportData"
        @actionsChange="getData"
      >
      </table-btns>
      <!-- <Button type="primary" icon="md-add" style="margin-right: 20px;" @click="addCommodity(0)">新增</Button>
            <Button type="primary" icon="md-add" @click="addCommodity(1)">批量新增</Button> -->
    </div>
    <!-- 表格内容 start  -->
    <Table
      ref="table"
      :columns="columns"
      :data="tableDataList"
      :loading="loading"
      height='500'
      class="ivu-mt"
      stripe
      @on-filter-change="handleFilterChange"
      @on-select="handleSelect"
      @on-select-cancel="handleSelectCancel"
      @on-select-all="handleSelectAll"
      @on-select-all-cancel="handleSelectAllCancel"
    >
      <template slot-scope="{ row }" slot="issueState">
        <span v-if="row.issueState == 1">发布</span>
        <span v-if="row.issueState == 2">未发布</span>
      </template>
      <template slot-scope="{ row, index }" slot="action">
        <table-actions
          :requestApi="listData.requestApi"
          :basePath="basePath"
          :rowData="row"
          :indexData="index"
          @actionsChange="getData"
        ></table-actions>
      </template>
    </Table>
    <!-- 表格内容 end  -->
    <!-- 分页 start  -->
    <div class="ivu-mt ivu-text-center">
      <Page
        :total="totalNum"
        transfer
        show-total
        show-elevator
        show-sizer
        :current.sync="nowPage"
        @on-change="getData"
        @on-page-size-change="handleChangePage"
        :page-size-opts="[10, 30, 50, 100]"
      />
    </div>
    <!-- 分页 end  -->
  </div>
</template>
<script>
import { mapState, mapMutations } from 'vuex';
import util from '@/libs/util';
import { ApiRequest } from '@api/basePage';
import tables from '@/mixins/dynamictable'; // 混入通用代码
import tableActions from './tableActions'; // 表格操作项
import tableBtns from './tableBtns'; // 表格内部操作按钮
import { getBtnsId } from '@/libs/btnsPermission';

export default {
  mixins: [tables],
  props: {
    dynamicData: {
      type: Object
    },
    pagePath: {
      type: String
    },
    orderState: {
      type: String,
      default: ''
    }
  },
  computed: {
    ...mapState('admin/baseList', [
      'listData',
      'searchFormData',
      'searchFormDataQ'
    ])
  },
  watch: {
    orderState (n, o) {
      this.getData();
    }
  },
  components: {
    tableActions,
    tableBtns
  },
  data () {
    return {
      util: util,
      basePath: this.dynamicData.basePath // 拼接基础路径
    };
  },
  created () {},
  methods: {
    ...mapMutations('admin/baseList', ['setTableRowData']),
    // 获取表格的数据
    getData () {
      let searchParam = [];
      Object.keys(this.searchFormData).forEach((key) => {
        searchParam.push({ name: key, value: this.searchFormData[key] });
      });
      Object.keys(this.searchFormDataQ).forEach((key) => {
        searchParam.push({ name: key, value: this.searchFormDataQ[key] });
      });

      let data = {
        nowPage: this.nowPage,
        pageShow: this.pageShow,
        searchParam: JSON.stringify(searchParam),
        sortName: '',
        sortType: ''
      };
      let curBtnId = getBtnsId('查询');
      sessionStorage.setItem('buttonId', curBtnId);
      this.loading = true;
      // 开始请求列表
      ApiRequest(
        this.listData.requestApi.getTableList.url,
        this.listData.requestApi.getTableList.type,
        data
      ).then((res) => {
        let rData = res;
        if (res.retStatus == 1) {
          if (res.retData) {
            let tableDataList = rData.retData.results;
            tableDataList.forEach((item) => {
              let actions = [];
              // 审核不通过现在原型不让修改
              // if (item.commitState === 'FAIL') {
              //     actions.push({
              //         name: 'Edit',
              //         label: '修改',
              //         handleType: 'link'
              //     });
              // }
              actions.push({
                name: 'View',
                label: '查看单据',
                handleType: 'link'
              });
              /*       if (item.commitState === 'COMMIT') {
                            actions.push({
                              name: 'AuditPass',
                              label: '模拟审核通过',
                              handleType: 'link'
                            });
                            actions.push({
                              name: 'AuditFail',
                              label: '模拟审核驳回',
                              handleType: 'link'
                            });
                          } */
              item.actions = actions;
            });
            // 增加表格操作项
            this.tableDataList = rData.retData.results;
            this.totalNum = rData.retData.totalNum;
            this.loading = false;
          } else {
            this.tableDataList = [];
            this.totalNum = 0;
            this.loading = false;
          }
        }
      });
    }
  }
};
</script>
<style lang="less" scoped>
.i-table-no-border .ivu-table-header th {
  background: #f8f8f8;
}
</style>
